package leetcode_周赛._2022._202205;

import general_class.TreeNode;

/**
 * @author yzh
 * @version 1.0
 * @date 2022/5/8 10:59
 */
public class _08 {
    public static void main(String[] args) {
        System.out.println(new _08().countTexts("22233"));
    }

    /**
     * 6057. 统计值等于子树平均值的节点数
     * 后序遍历
     */
    int cnt;

    public int averageOfSubtree(TreeNode root) {
        dfs(root);
        return cnt;
    }
    int[] dfs(TreeNode node) {
        if (node == null) return new int[]{0, 0};
        int[] left = dfs(node.left);
        int[] right = dfs(node.right);
        int sum = left[0] + right[0] + node.val, nodeCnt = left[1] + right[1] + 1;
        if (node.val == sum / nodeCnt) ++cnt;
        return new int[]{sum, nodeCnt};
    }

    /**
     * 6058. 统计打字方案数
     * 计算连续 2、3...9 有多少种情况, 类似爬楼梯
     * 遍历 pressedKeys, 找出连续的数字
     * 数字 7、9 有四个字母
     */
    static int N = 100010, MOD = (int) 1e9 + 7;
    static long[] three = new long[N], four = new long[N];

    static {
        three[0] = 1;
        three[1] = 1;
        three[2] = 2;
        three[3] = 4;
        four[0] = 1;
        four[1] = 1;
        four[2] = 2;
        four[3] = 4;
        for (int i = 4; i < N; i++) {
            three[i] = (three[i - 1] + three[i - 2] + three[i - 3]) % MOD;
            four[i] = (four[i - 1] + four[i - 2] + four[i - 3] + four[i - 4]) % MOD;
        }
    }

    public int countTexts(String pressedKeys) {
        int i = 0, len = pressedKeys.length();
        long ans = 1;
        while (i < len) {
            char c = pressedKeys.charAt(i);
            int cnt = 0;
            while (i < len && c == pressedKeys.charAt(i)) {
                ++cnt;
                ++i;
            }
            if (c == '7' || c == '9') ans = (ans * four[cnt]) % MOD;
            else ans = (ans * three[cnt]) % MOD;
        }
        return (int) ans;
    }

}
